export default {
    state: {
        // 登录状态
        loginStatus: false,
        // token
        token: null,
        // 用户信息
        userInfo: {}
    },
    mutations: {
        // 初始化登录
        initUser(state) {
            let userinfo = window.localStorage.getItem("userInfo");
            if (userinfo) {
                userinfo = JSON.parse(userinfo);
                state.loginStatus = true;
                state.token = userinfo.token;
                state.userInfo = userinfo
            }
        },
        // 登录
        login(state, userinfo) {
            state.userInfo = userinfo
            state.loginStatus = true
            state.token = userinfo.token
            // 持久化存储
            window.localStorage.setItem('userInfo', JSON.stringify(userinfo))
        },
        //退出登录
        loginout(state) {
            state.userInfo = {}
            state.loginStatus = false
            state.token = null
            // 持久化存储
            window.localStorage.removeItem('userInfo')
        }
    }
}